mini.parse();

function CouponFormHandler() {
};

CouponFormHandler.instance = new CouponFormHandler();

//获得全局的购物券表单对象
CouponFormHandler.couponForm = new mini.Form("couponForm");
CouponFormHandler.formData = {};
//全局id
CouponFormHandler.__bciId = null;

// 购物券状态数据
CouponFormHandler.bciStateData = [
	{text: "有效",value: 1},
	{text: "无效",value: 0}
];
mini.get("bciState").setData(CouponFormHandler.bciStateData);

//创建上传购物券图片控件
SWFUploadTools.createSWFUBtn(__realpath+__couponImgPath, null, "bciUpload", uploadBciPicurlSuccess);

/**
 * 保存（添加或编辑）数据的方法
 */
CouponFormHandler.save = function(){
	var couponData = CouponFormHandler.couponForm.getData();
	couponData.bciId = CouponFormHandler.formData.bciId;
	
    CouponFormHandler.couponForm.validate();
    if (CouponFormHandler.couponForm.isValid() == false) return;
    
    var json = mini.encode(couponData);
    $.ajax({
        url: __root + "/saveOrUpdateCoupon.do",
        type: "POST",
        data: {
        	couponInfo: json,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        cache: false,
        success: function (text) {
        	MiniuiTools.miniInfo("系统提示","保存成功！", function(action){
        		CouponFormHandler.closeCurrentWin("save");
        	});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", jqXHR.responseText, function(){
            	CouponFormHandler.closeCurrentWin();
            });
        }
    });
    
}

/**
 * 标准方法接口定义
 * @param {} data
 */
CouponFormHandler.passData = function(data) {
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        CouponFormHandler.formData.bciId = data.bciId;
        //根据id获取对应的购物券信息
        $.ajax({ 
            url: __root + "/getCouponInfoById.do?bciId=" + data.bciId,
            cache: false,
            dataType: 'json',
            success: function (text) {
            	//设置全局id
            	CouponFormHandler.__bciId = data.bciId;
            	//设置form表单
                CouponFormHandler.couponForm.setData(text);
                
                if(text.bciPicurl != "" && text.bciPicurl.length > 0){
                	$("#imgViewer").empty();
					
					var iframe = "<iframe src='" + __root + "/imgViewerContorller.do?basePath=" + __realpath+__couponImgPath + "&newImgPath=" + text.bciPicurl + "' frameborder='0' border='0' style='margin:0;padding:0;width:100%;height:100%;'/>";
					$("#imgViewer").append(iframe);
                }
            }
        });
    }else{
    	//默认购物券状态为有效
    	mini.get("bciState").setValue(1);
    }
}

/**
 * 关闭当前窗口
 * @param {} action
 */
CouponFormHandler.closeCurrentWin = function(action) {
    if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);
    else window.close();
    CouponFormHandler.__bciId = null;
}

/**
 * 关闭mini窗口
 */
CouponFormHandler.closeMiniWin = function(){
	CouponFormHandler.closeCurrentWin("cancel");
	CouponFormHandler.__bciId = null;
}

/**
 * 处理上传成功后的图片
 * @param {} file
 * @param {} serverData
 */
function uploadBciPicurlSuccess(file, serverData) {
	MiniuiTools.miniHideLoading();
	MiniuiTools.miniInfo("系统提示","文件上传成功！");
	
	$("#imgViewer").empty();
	var fdata = mini.decode(serverData);
	var newPath = fdata.imgPath;	
	var iframe = "<iframe src='" + __root + "/imgViewerContorller.do?basePath=" + __realpath+__couponImgPath + "&newImgPath=" + newPath + "' frameborder='0' border='0' style='margin:0;padding:0;width:100%;height:100%;'/>";
	$("#imgViewer").append(iframe);
	
	mini.get("upload_bciPicurl").setValue(newPath);
}

function vldBicPrice(e){
	if(!XMAppUtils.isRMB(e.value)){
		e.errorText = "无效的数值金额！";
        e.isValid = false;
	}
}